package fr.upstlse.glre.client.espacededie.commun.communiquer;

import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;

import com.extjs.gxt.ui.client.Events;
import com.extjs.gxt.ui.client.binder.DataListBinder;
import com.extjs.gxt.ui.client.data.ModelStringProvider;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.DataList;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;

import fr.upstlse.glre.client.constante.fields.MessageConstante;
import fr.upstlse.glre.client.utilitaires.ControlerUtil;
import fr.upstlse.glre.client.vo.communiquer.BoiteAuxLettresVOGWT;
import fr.upstlse.glre.client.vo.communiquer.MessageEtatGWT;
import fr.upstlse.glre.client.vo.communiquer.MessageVOGWT;
import fr.upstlse.glre.client.widgets.modelData.MessageModelData;

public class Lire extends LayoutContainer {

	private DataList lirePanel = new DataList();

	private Collection<MessageVOGWT> messageVOs;

	private Listener<ComponentEvent> viewerSelectionAction;
	ListStore<MessageModelData> store;

	private static Lire instance;

	public Lire() {
		instance = this;
	}

	@Override
	protected void onRender(Element pParent, int pIndex) {
		super.onRender(pParent, pIndex);
		createContentPanel();
	}

	private void createContentPanel() {
		setAutoWidth(true);
		setWidth(500);
		setId(MessageConstante.BOITEAUXLETTES_PANEL_ID.getValue());
		setBorders(true);
		createPanel();
		instance.add(lirePanel);
		instance.layout();
	}

	public void createPanel() {
		lirePanel.setFlatStyle(true);

		final MessageBox lAttente = MessageBox.wait("Chargement",
				"Chargement des messages...", "Veuillez patienter");

		AsyncCallback<BoiteAuxLettresVOGWT> lCallback = new AsyncCallback<BoiteAuxLettresVOGWT>() {

			public void onFailure(Throwable pCaught) {
				lAttente.hide();
				MessageBox.alert("Erreur findByProprietaire ", pCaught
						.getMessage(), null);
			}

			public void onSuccess(BoiteAuxLettresVOGWT pResult) {
				lAttente.hide();
				messageVOs = pResult.getMessages();
				removeAncetres();

				store = new ListStore<MessageModelData>();
				store.add(MessageModelData.toMessageModelDatas(messageVOs));

				DataListBinder<MessageModelData> lBinder = new DataListBinder<MessageModelData>(
						lirePanel, store);
				lBinder.setDisplayProperty(MessageConstante.MODELDATA_TITRE.getValue());
				lBinder.setStringProvider(new ModelStringProvider<MessageModelData>() {

					public String getStringValue(MessageModelData model,
							String property) {
						String lTexte = (String) model.get(MessageConstante.MODELDATA_TITRE.getValue());
						return lTexte;
					}

				});
				lBinder.setStyleProvider(new ModelStringProvider<MessageModelData>() {

					public String getStringValue(MessageModelData pModel,
							String pProperty) {
						String lStyle = "nouveau";
						MessageEtatGWT lMessageEtatGWT = (MessageEtatGWT) pModel.get(MessageConstante.MODELDATA_ETAT.getValue());
						if(lMessageEtatGWT.equals(MessageEtatGWT.LU)) {
							lStyle = "lu";
						}
						return lStyle;
					}
					
				});
				lBinder.init();

				lirePanel.addListener(Events.SelectionChange,
						viewerSelectionAction);

				Info.display("Information",
						"Succès du chargement des messages", "");
			}

		};
		ControlerUtil.getUserControlerServiceAsyncInstance()
				.findByProprietaire(Message.getInstance().getUserVOGWT(),
						lCallback);

	}

	public DataList getLirePanel() {
		return lirePanel;
	}

	public void setLirePanel(DataList pLirePanel) {
		lirePanel = pLirePanel;
	}

	public Listener<ComponentEvent> getViewerSelectionAction() {
		return viewerSelectionAction;
	}

	public void setViewerSelectionAction(
			Listener<ComponentEvent> viewerSelectionAction) {
		this.viewerSelectionAction = viewerSelectionAction;
	}

	public ListStore<MessageModelData> getStore() {
		return store;
	}

	public void setStore(ListStore<MessageModelData> store) {
		this.store = store;
	}
	
	public static Lire getInstance() {
		return instance;
	}
	
	private void removeAncetres() {
		Collection<MessageVOGWT> lAllAncetres = new HashSet<MessageVOGWT>();
		for(MessageVOGWT lMessageVOGWT : messageVOs) {
			List<MessageVOGWT> lAncetres = lMessageVOGWT.getAncetres();
			if(!lAncetres.isEmpty()) {
				lAllAncetres.addAll(lAncetres);
			}
		}
		
		List<MessageVOGWT> lToRemove = new LinkedList<MessageVOGWT>();
		for (MessageVOGWT lMessageVOGWT : messageVOs) {
			for (MessageVOGWT lMessageVOGWT2 : lAllAncetres) {
				if (lMessageVOGWT.getId() == lMessageVOGWT2.getId()) {
					lToRemove.add(lMessageVOGWT);
				}
			}
		}
		
		messageVOs.removeAll(lToRemove);
	}
}
